/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Console.java
 *
 * Created on Nov 18, 2009, 8:23:56 PM
 */
package wekiinputhelper.gui;

import java.awt.Color;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import wekiinputhelper.WekiInputHelper;

//TODO: implement ability to see recent errors when window opened
//Tie to log file: show last N lines of file in console window when window opened for 1st time

/**
 *
 * @author rebecca
 */
public class Console extends javax.swing.JFrame {

    //private final WekinatorConsoleHandler h;
    private final WekiInputHelper w;
    private static final Logger logger = Logger.getLogger(Console.class.getName());
    private final StyledDocument doc;
    private final SimpleAttributeSet regular = new SimpleAttributeSet();
    private final SimpleAttributeSet warning = new SimpleAttributeSet();

    /** Creates new form Console
      Just for testing GUI layout */
    public Console() {
       initComponents();
       doc = textPane.getStyledDocument();
       w = null;
       initStyles();
    }

    /** Creates new form Console */
    public Console(WekiInputHelper w) {
       initComponents();
       doc = textPane.getStyledDocument();

       this.w = w;
       initStyles();
       
      // h = new WekinatorConsoleHandler();
      // h.setTextArea(text1);
       w.getLoggingManager().startLoggingToConsoleGUI(this);
    }
    
    private void initStyles() {
        StyleConstants.setFontFamily(regular, "SansSerif");
        
        StyleConstants.setFontFamily(warning, "SansSerif");
        StyleConstants.setForeground(warning, Color.red);
    }

    public void log(String s) {
        if (isVisible()) {
            try {
                textPane.getStyledDocument().insertString(textPane.getStyledDocument().getLength(), s+"\n", regular);
                textPane.setCaretPosition(textPane.getStyledDocument().getLength());
            } catch (BadLocationException ex) {
            }
        }
    }
    
    public void logWarning(String s) {
        if (isVisible()) {
            try {
                textPane.getStyledDocument().insertString(textPane.getStyledDocument().getLength(), s+"\n", warning);
                textPane.setCaretPosition(textPane.getStyledDocument().getLength());
            } catch (BadLocationException ex) {
            }
        }
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        textScrollPane = new javax.swing.JScrollPane();
        textPane = new javax.swing.JTextPane();
        buttonClear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Wekinator Input Helper Console");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        textPane.setEditable(false);
        textPane.setContentType("text/html"); // NOI18N
        textScrollPane.setViewportView(textPane);

        buttonClear.setText("Clear");
        buttonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(0, 346, Short.MAX_VALUE)
                .add(buttonClear))
            .add(textScrollPane)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(textScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(buttonClear))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed
        try {
            doc.remove(0, doc.getLength());
        } catch (BadLocationException ex) {
        }
    }//GEN-LAST:event_buttonClearActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        //Remove the handler
        w.getLoggingManager().stopLoggingToConsoleGUI();
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Console b = new Console();
                b.setVisible(true);
            //
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClear;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextPane textPane;
    private javax.swing.JScrollPane textScrollPane;
    // End of variables declaration//GEN-END:variables


    @Override
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

}
